package com.blb.service.impl;

import com.blb.entity.OrderLog;
import com.blb.entity.ProductHandover;
import com.blb.mapper.ProductHandoverMapper;
import com.blb.service.IOrderLogService;
import com.blb.service.IProductHandoverService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tb
 * @since 2020-05-29
 */
@Service
public class ProductHandoverServiceImpl extends ServiceImpl<ProductHandoverMapper, ProductHandover> implements IProductHandoverService {

    @Resource
    private ProductHandoverMapper productHandoverMapper;

    @Autowired
    private IOrderLogService orderLogService;

    /**
     * 交接单列表
     * @return
     */
    @Override
    public List<ProductHandover> Ilist(Integer start, Integer length,String companyId) {
        return productHandoverMapper.Ilist(start, length,companyId);
    }

    /**
     * 根据订单号查询列表
     * @param orderNo
     * @return
     */
    @Override
    public List<ProductHandover> queryByOrderNo(String orderNo,String companyId) {
        return productHandoverMapper.queryByOrderNo(orderNo,companyId);
    }

    /**
     * 查看订单号是否存在
     * @param orderNo
     * @return
     */
    @Override
    public int checkOrderNo(String orderNo) {
        return productHandoverMapper.checkOrderNo(orderNo);
    }

    /**
     * 添加交接单和订单日志
     * @param productHandover
     * @return
     */
    @Override
    @Transactional
    public boolean saveAndLog(ProductHandover productHandover) {
        boolean save = save(productHandover);
        OrderLog orderLog = new OrderLog();
        orderLog.setOrderTime(TimeUtils.getNowTime());
        orderLog.setOrderId(productHandover.getOrderNo());
        orderLog.setProduction("入库完成");
        boolean saveLog = orderLogService.save(orderLog);
        return saveLog && save;
    }

    /**
     * 查询总数
     * @param companyId
     * @return
     */
    @Override
    public int getCount(String companyId) {
        return productHandoverMapper.getCount(companyId);
    }
}
